﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Add</title>
    <link href="~/Content/lib/layui/css/layui.css" rel="stylesheet" />
    <script src="~/Content/lib/layui/layui.js"></script>
</head>
<body>
    <form id="fAdd" class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">产品名称</label>
            <div class="layui-input-inline">
                <input type="text" name="name" id="name" required lay-verify="required" placeholder="请输入产品名称" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">产品品牌</label>
            <div class="layui-input-inline">
                <input type="text" name="brand" id="brand" required lay-verify="required" placeholder="请输入产品品牌" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">产品规格</label>
            <div class="layui-input-inline">
                <input type="text" name="specification" id="specification" required lay-verify="required" placeholder="请输入产品规格" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">产品介绍</label>
            <div class="layui-input-inline">
                <input type="text" name="info" id="info" required lay-verify="required" placeholder="请输入产品介绍" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">产品价格</label>
            <div class="layui-input-inline">
                <input type="text" name="price" id="price" required lay-verify="required" placeholder="请输入产品价格" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">产品型号</label>
            <div class="layui-input-inline">
                <input type="text" name="model" id="model" required lay-verify="required" placeholder="请输入产品价格" autocomplete="off" class="layui-input">
            </div>
        </div>

        @*<div class="layui-form-item">
            <label class="layui-form-label">商品默认图片</label>
            <div class="layui-input-inline">
                <input type="text" name="picUrl" required lay-verify="required" placeholder="请输入商品默认图片" autocomplete="off" class="layui-input">
            </div>
        </div>*@

        <div class="layui-form-item">
            <label class="layui-form-label">产品默认图片</label>
            <div class="layui-input-block">
                <input type="file" id="my_file" name="file">
                <input hidden type="text" id="image" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <div class="layui-input-block">
                <img id="my_img" style="width:200px;height:200px" />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否显示在首页</label>
            <div class="layui-input-inline">
                <select id="isShowHome" name="isShowHome">
                    <option value=""></option>
                    <option value="1">是</option>
                    <option value="0">否</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">产品类型</label>
            <div class="layui-input-inline">
                <select id="category_id" name="category_id">
                    <option value="">---请选择产品类型---</option>
                    @foreach (var item in Model)
                    {
                        <option value="@item.id">@item.TypeName</option>
                    }
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <script>
        //Demo
        layui.use(['form', 'upload', 'laydate'], function () {
            var form = layui.form;
            var upload = layui.upload;
            var $ = layui.$;
            var laydate = layui.laydate;

            laydate.render({
                elem: '#create_time'
            });
            upload.render({
                elem: "#my_file",
                url: "/Back/Home/UploadImg",
                done: function (res) {
                    if (res.code == 0) {
                        $("#my_img").attr("src", "/images/" + res.path);
                        $("#image").val(res.path);
                    }
                }
            })
            //监听提交
            form.on('submit(formDemo)', function (data) {
                $.ajax({
                    url: "/Back/Product/Add",
                    type: "post",
                    data: {
                        name: $("#name").val(),
                        brand: $("#brand").val(),
                        info: $("#info").val(),
                        price: $("#price").val(),
                        category_id: $("#category_id").val(),
                        specification: $("#specification").val(),
                        image: $("#image").val(),
                        is_ShowHome: $("#isShowHome").val(),
                        model: $("#model").val()
                    },
                    dataType: "json",
                    success: function (res) {
                        if (res.suc) {
                            layer.alert(res.msg);
                            //当你在iframe页面关闭自身时
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        } else {
                            layer.alert(res.msg);
                            window.parent.location.reload();
                        }
                    }
                })
                return false;
            });
        });
    </script>
</body>
</html>
